<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>事件委托</title>
    <style>
        .active
        {
            color: red;
        }
    </style>
</head>
<body>

    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
    </ul>

    <script>
         
        //  var ulEl = document.querySelector("ul");
        //  ulEl.onclick = function(event)
        //  {
        //     event.target.classList.add("active");
        //  }

        var ulEl = document.querySelector("ul");

        ulEl.onclick= function()
        {
            var lastEl = document.querySelector(".active");
            
            if(lastEl)
            {
                lastEl.classList.remove("active");
            }
            event.target.classList.add("active");
        }

    </script>
    
</body>
</html>